//
//  ForgetPswViewController.m
//  witMorning
//
//  Created by 信晨汇 on 2018/1/9.
//  Copyright © 2018年 zt. All rights reserved.
//

#import "ForgetPswViewController.h"

@interface ForgetPswViewController ()<UITextFieldDelegate>

@property(nonatomic,strong)NSString *pwd;
@property(nonatomic,strong)NSString *againPwd;
@property(nonatomic,strong)NSString *phoneNum;

@end

@implementation ForgetPswViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    
    
    UIButton *closeBtn = [[UIButton alloc]initWithFrame:CGRectMake(30, 200, 30, 30)];
    [closeBtn setImage:[UIImage imageNamed:@"delete"] forState:UIControlStateNormal];
    [closeBtn addTarget:self action:@selector(closeClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:closeBtn];
    
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(100, 200, SCREEN_W-200, 30)];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.font = [UIFont systemFontOfSize:20];
    titleLabel.textColor = [UIColor grayColor];
    titleLabel.text = @"密码找回";
    [self.view addSubview:titleLabel];
    
    NSArray *labelArray = @[@"新密码：",@"确认密码：",@"手机号码："];
    NSArray *placeholderArray = @[@"请输入新密码",@"请确认新密码",@""];
    
    for (int i = 0; i<labelArray.count; i++) {
        
        CGRect rect = CGRectMake(10, 250+50*i, SCREEN_W-20, 40);
        UIView *contentView = [self contentViewWithRect:rect name:labelArray[i] placehoder:placeholderArray[i] tag:i];
        [self.view addSubview:contentView];
        
    }
    
    
    //    button
    CGFloat y = 250 + 3*50;
    UIButton *confirmBtn = [[UIButton alloc]initWithFrame:CGRectMake(70, y+20, SCREEN_W-140, 45)];
    confirmBtn.backgroundColor = [UIColor blueColor];
    [confirmBtn setTitle:@"确定" forState:UIControlStateNormal];
    [confirmBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    confirmBtn.titleLabel.font = [UIFont systemFontOfSize:17];
    confirmBtn.layer.cornerRadius = 5;
    confirmBtn.clipsToBounds = YES;
    [confirmBtn addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:confirmBtn];
    
    
}

-(void)closeClick:(UIButton *)sender{
    
    [self dismissViewControllerAnimated:YES completion:nil];
    
}

-(void)buttonClick:(UIButton *)sender{
    
    if (!self.pwd || !self.againPwd || ![self.pwd isEqualToString:self.againPwd]) {
        [MBProgressHUD showMessag:@"请填写正确格式密码" toView:self.view];
        return;
    }
    
    NSString *str = [BASE_HTTP stringByAppendingString:@"updatePwd"];
    [[WMManage shareInstance]postRequestWithUrl:str andParam:@{@"user.phone":self.phoneNum?self.phoneNum:@"",@"user.password":self.pwd?self.pwd:@""} success:^(id responseObj) {
        NSLog(@"%@",responseObj);
        if (responseObj[@"result"] && [responseObj[responseObj] isEqualToString:@"success"]) {
            [MBProgressHUD showMessag:@"找回密码成功" toView:self.view];
            [self closeClick:nil];
        }
    } fail:^(id error) {
        NSLog(@"%@",error);
        [MBProgressHUD showMessag:@"请求失败" toView:self.view];
    }];
    
}

-(UIView *)contentViewWithRect:(CGRect)rect name:(NSString *)title  placehoder:(NSString *)placehoder tag:(NSInteger)tag{
    
    UIView *viewview = [[UIView alloc]initWithFrame:rect];
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 15, 80, 30)];
    label.text = title;
    label.font = [UIFont systemFontOfSize:14];
    label.textColor = [UIColor grayColor];
    label.textAlignment = NSTextAlignmentRight;
    [viewview addSubview:label];
    
    UITextField *textf = [[UITextField alloc]initWithFrame:CGRectMake(label.right+10, 15, rect.size.width-80-30, 30)];
    textf.placeholder = placehoder;
    textf.tag = tag;
    textf.font = [UIFont systemFontOfSize:16];
    textf.delegate = self;
    [viewview addSubview:textf];
    
    
    UILabel *lineLabel = [[UILabel alloc]initWithFrame:CGRectMake(textf.x, textf.bottom, textf.width, 1)];
    lineLabel.backgroundColor = [UIColor lightGrayColor];
    [viewview addSubview:lineLabel];
    
    
    return viewview;
    
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSString *str = [textField.text stringByAppendingString:string];
    if (textField.tag == 0) {
        self.pwd = str;
    } else if (textField.tag == 1) {
        self.againPwd = str;
    }else if (textField.tag == 50) {
        self.phoneNum = str;
        if (str.length > 11) {
            return NO;
        }
    }
    return YES;
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
